﻿#pragma execution_character_set("utf-8")

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<iostream>
#include<QTextCodec>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(codec);
    ui->setupUi(this);
    ui->centralWidget->setMouseTracking(true);
    QIcon plus_icon;
    plus_icon.addFile(tr(":/image/plus.png"));
    ui->addButton->setIcon(plus_icon);

    plus_icon.addFile(tr(":/image/delete.ico"));
    ui->deleteButton->setIcon(plus_icon);


    QTableWidget *leaveTable = ui->tableWidget;
    leaveTable->setRowCount(9);
    leaveTable->setColumnWidth(0,leaveTable->width()*0.2);
    leaveTable->setColumnWidth(1,leaveTable->width()*0.59);
    leaveTable->setColumnWidth(2,leaveTable->width()*0.16);
    leaveTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);

    //TODO get date and init the table


    if(!dbc.connect(DB_NAME)){
        std::cout<<"db connect wrong"<<std::endl;
    }

    mm.initFromJsonString(dbc.getValue(KEY_MEMBER));
    //dl.initFromJsonString()
    md = new MemberDialog(&mm);
    ld= new AddLeaveDialog(&mm,&dl);
    connect(md,&MemberDialog::saveMember,this,&MainWindow::on_saveMember);
    connect(ld,&AddLeaveDialog::saveLeaves,this,&MainWindow::on_saveLeaves);
}



MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::setTable(){

    //emit clearTableContens();
    ui->tableWidget->clearContents();
    QDate date =  ui->calendarWidget->selectedDate();
    std::string ds= date.toString(DATE_FORMATE).toStdString();
    dl.initFromJsonString(ds,dbc.getValue(ds));
    //TODO
    ui->tableWidget->setRowCount(dl.count());
    ui->deleteButton->setEnabled(false);

    for (int i=0;i<dl.count();i++){
        QColor c;
        if(i%2==0){
            c= GRAY;
        }else{
            c= WHITE;
        }

        QTableWidgetItem *name = new QTableWidgetItem(QString::fromStdString(dl.getLeave(i).m_name));
        name->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
        name->setBackgroundColor(c);
        name->setTextColor(BLACK);
        name->setFont(QFont("Helvetica",13));
        ui->tableWidget->setItem(i,0,name);

        QTableWidgetItem *reasonstr = new QTableWidgetItem(QString::fromStdString(dl.getLeave(i).reason));
        reasonstr->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
        reasonstr->setBackgroundColor(c);
        reasonstr->setTextColor(BLACK);
        reasonstr->setFont(QFont("Helvetica",13));
        ui->tableWidget->setItem(i,1,reasonstr);

        int t=dl.getLeave(i).time;
        std::string ts;
        switch (t) {
        case MORNING:
            ts="上午";
            break;
        case AFTER:
            ts ="下午";
            break;
        case ALL:
            ts = "全天";
            break;
        default:
            break;

        }
        QTableWidgetItem *time = new QTableWidgetItem(QString::fromStdString(ts));
        time->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
        time->setBackgroundColor(c);
        time->setTextColor(BLACK);
        time->setFont(QFont("Helvetica",13));
        ui->tableWidget->setItem(i,2,time);
    }
}


void MainWindow::on_calendarWidget_clicked(const QDate &date)
{
    std::string dstr= date.toString(DATE_FORMATE).toStdString();
    dl.initFromJsonString(dstr,dbc.getValue(dstr));
    this->setTable();
}

void MainWindow::on_saveMember(){
    dbc.setValue(KEY_MEMBER,mm.toJson());
}

void MainWindow::on_saveLeaves(){
    dbc.setValue(dl.getDateStr(),dl.toJson());
}



void MainWindow::showEvent(QShowEvent *event){
    this->setTable();

    std::cout<<"show Event"<<std::endl;
}


void MainWindow::on_addButton_clicked()
{
    ld->exec();
    this->setTable();
}

void MainWindow::on_deleteButton_clicked()
{
    if(ui->tableWidget->selectedItems().count()!=0){

        dl.removeLeave(ui->tableWidget->selectedItems()[0]->row());
        ui->tableWidget->removeRow(ui->tableWidget->selectedItems()[0]->row());
        ui->tableWidget->setCurrentItem(NULL);
        ui->deleteButton->setEnabled(false);
        this->on_saveLeaves();
    }
}

void MainWindow::on_memButton_clicked()
{
    md->exec();

}

void MainWindow::on_tableWidget_itemSelectionChanged()
{
    ui->deleteButton->setEnabled(true);
}

void MainWindow::on_pushButton_clicked()
{
    //TODO 导出月请假汇总


}
